package logservice

import (
	"fmt"
	lts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/model"

	regionv2 "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/region"
)

type LogStreamGetter interface {
	LogStream(region string) LogStreamInterface
}

type LogStreamInterface interface {
	CreateLogStream(name string, groupId string) (id string, err error)
	CreateLogStreamIndex(groupId string, streamId string) (result string, err error)
}

type logStream struct {
	client *lts.LtsClient
	region string
}

func newLogStream(log *LogService, region string) LogStreamInterface {
	_client := lts.NewLtsClient(
		lts.LtsClientBuilder().
			WithRegion(regionv2.ValueOf(region)).
			WithCredential(log.credentials).
			Build())
	return &logStream{
		client: _client,
		region: region,
	}
}

func (s *logStream) CreateLogStream(name string, groupId string) (id string, err error) {
	request := &model.CreateLogStreamRequest{}
	request.LogGroupId = groupId
	ttlInDaysCreateLogStreamParams := int32(3)
	request.Body = &model.CreateLogStreamParams{
		TtlInDays:     &ttlInDaysCreateLogStreamParams,
		LogStreamName: name,
	}
	response, err := s.client.CreateLogStream(request)
	if err != nil {
		fmt.Printf("[LogService] 创建日志流失败，%v", err)
		return
	}
	return *response.LogStreamId, nil
}

func (s *logStream) CreateLogStreamIndex(groupId string, streamId string) (result string, err error) {
	request := &model.CreateLogStreamIndexRequest{}
	request.GroupId = groupId
	request.StreamId = streamId
	caseSensitiveFields := false
	includeChineseFields := true
	quickAnalysisFields := true
	var listFieldsbody = []model.LtsFieldsInfo{
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "clusterId",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "nameSpace",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "appName",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "podName",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "podIP",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "hostIP",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "time",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		//{
		//	FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
		//	FieldName:      "content",
		//	CaseSensitive:  &caseSensitiveFields,
		//	IncludeChinese: &includeChineseFields,
		//	Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
		//	QuickAnalysis:  &quickAnalysisFields,
		//},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "grayVersion",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "level",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "messageData",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "printMessage",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "projectName",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "scenarios",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "tid",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "stack",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
		{
			FieldType:      model.GetLtsFieldsInfoFieldTypeEnum().STRING,
			FieldName:      "timestamp",
			CaseSensitive:  &caseSensitiveFields,
			IncludeChinese: &includeChineseFields,
			Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
			QuickAnalysis:  &quickAnalysisFields,
		},
	}
	fullTextIndexbody := &model.LtsFullTextIndexInfo{
		Enable:         true,
		CaseSensitive:  false,
		IncludeChinese: true,
		Tokenizer:      ", '\";=()[]{}@&<>/:\\n\\t\\r",
	}
	request.Body = &model.LtsIndexConfigInfo{
		Fields:        &listFieldsbody,
		FullTextIndex: fullTextIndexbody,
	}
	response, err := s.client.CreateLogStreamIndex(request)
	if err != nil {
		fmt.Printf("[LogService] 创建日志索引失败: %v\n", err)
		return
	}
	return *response.Result, nil
}

func (s *logStream) DeleteLogStream(groupId string, streamId string) {
	request := &model.DeleteLogStreamRequest{}
	request.LogGroupId = groupId
	request.LogStreamId = streamId
	response, err := s.client.DeleteLogStream(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

func (s *logStream) DescribeLogStream(id string) {

}

func (s *logStream) ListLogStreamByName(name string) {

}
func (s *logStream) ListAll() {

}

func (s *logStream) ListLogStream(groupId string) (*[]model.LogStreamResBody, error) {
	request := &model.ListLogStreamRequest{}
	request.LogGroupId = groupId
	response, err := s.client.ListLogStream(request)
	if err != nil {
		return response.LogStreams, err
	}
	return response.LogStreams, nil
}
